function [edit_output, edit_flag] = guiedit(edit_title, edit_input);

% GUIEDIT - create window for editing array values
%
%    [OUTPUT, FLAG] = GUIEDIT(TITLE, INPUT) creates a window named TITLE
%    where the values contained in the array INPUT can be individually
%    edited.  The edited array is returned in OUTPUT, and FLAG indicates
%    how the edit window was exited (0 if Cancel, 1 if OK).

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: May 2, 2000

% initialize output

edit_output = edit_input;
edit_flag = 0;

% check input parameters

if (nargin ~= 2)
   msgbox('Invalid number of arguments', 'GUIEDIT Error', 'warn');
   return
elseif (~ischar(edit_title) | isempty(edit_title))
   msgbox('TITLE must be a character string', 'GUIEDIT Error', 'warn');
   return
elseif (isnumeric(edit_input))
   numeric_flag = 1;
elseif (iscell(edit_input))
   if (all(cellfun('isclass', edit_input, 'char')))
      numeric_flag = 0;
   else
      msgbox('INPUT must be a cell array of strings', 'GUIEDIT Error', 'warn');
      return
   end
else
   msgbox('INPUT must be a numeric or cell array', 'GUIEDIT Error', 'warn');
   return
end

% calculate size & location of figure window

fig_height = min(0.8, (0.1 + 0.035*size(edit_input, 1)));
fig_width = min(0.8, (0.05 + 0.075*size(edit_input, 2)));
edit_pos = [(1 - fig_width)/2 (1 - fig_height)/2 fig_width fig_height];

% calculate size & spacing of edit boxes
% horizontal space = edit box width / 4
% vertical space = edit box height / 2

edit_width = 4/(5*size(edit_input, 2) + 1);
space_width = edit_width/4;
edit_height = 2/(3*size(edit_input, 1) + 6);
space_height = edit_height/2;

% calculate size & spacing of OK & Cancel buttons

button_height = 3*edit_height/2;
button_width = min(0.4, 3*edit_width/2);
button_left = (1 - 2*button_width)/3;
button_right = 2*button_left + button_width;

% create a new figure window to hold the array editor

h_fig = figure('Color', [0.8 0.8 0.8], ...
   'Units', 'normalized', ...
   'Position', edit_pos, ...
   'MenuBar', 'none', ...
   'Name', edit_title, ...
   'NumberTitle', 'off', ...
   'CloseRequestFcn', 'set(gcf, ''UserData'', 0)');

% create array of edit box handles to correspond with array elements
% loop through array elements to create edit boxes
% now capable of editing cell array string values

h_edit = zeros(size(edit_input));
y_pos = 1;
for i = 1 : size(edit_input, 1)
   x_pos = space_width;
   y_pos = y_pos - edit_height - space_height;
   for j = 1 : size(edit_input, 2)
      if (numeric_flag)
         edit_string = sprintf('%g', edit_input(i, j));
      else
         edit_string = edit_input{i, j};
      end
      h_edit(i, j) = uicontrol('Parent', h_fig, ...
         'Style', 'edit', ...
         'Units', 'normalized', ...
         'Position', [x_pos y_pos edit_width edit_height], ...
         'BackgroundColor', [1 1 1], ...
         'ForegroundColor', [0 0 0], ...
         'Min', 0, 'Max', 1, ...
         'String', edit_string);
      x_pos = x_pos + edit_width + space_width;
   end
end

% create separator
% create 'OK' & 'Cancel' buttons

y_pos = y_pos - space_height;
h_frame = uicontrol('Parent', h_fig, ...
   'Style', 'frame', ...
   'Units', 'normalized', ...
   'Position', [0 y_pos 1 edit_height/20]);

y_pos = y_pos - button_height - space_height;
h_ok = uicontrol('Parent', h_fig, ...
   'Style', 'pushbutton', ...
   'Units', 'normalized', ...
   'Position', [button_left y_pos button_width button_height], ...
   'Callback', 'set(gcf, ''UserData'', 1)', ...
   'String', 'OK');

h_cancel = uicontrol('Parent', h_fig, ...
   'Style', 'pushbutton', ...
   'Units', 'normalized', ...
   'Position', [button_right y_pos button_width button_height], ...
   'Callback', 'set(gcf, ''UserData'', 0)', ...
   'String', 'Cancel');

% wait for user to click button or close edit figure
% copy array values if selected

waitfor(h_fig, 'UserData');
edit_flag = get(h_fig, 'UserData');
if (edit_flag)
   for i = 1 : size(edit_output, 1)
      for j = 1 : size(edit_output, 2)
         if (numeric_flag)
            edit_output(i, j) = str2num(get(h_edit(i, j), 'String'));
         else
            edit_output{i, j} = get(h_edit(i, j), 'String');
         end
      end
   end
end

% delete edit figure

if (istype(h_fig, 'figure'))
   delete(h_fig);
end
return
